﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Reflection;

namespace Lib.Data
{
    public class DataTableToList
    {
        public static List<T> DataTableToLista<T>(DataTable dt) where T : class,new()
        {

            List<T> list = new List<T>();
            string TempName = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] pro = t.GetType().GetProperties();
                foreach (PropertyInfo item in pro)
                {
                    TempName = item.Name;
                    if (dt.Columns.Contains(TempName))
                    {
                        object value = dr[TempName];
                        if (value != DBNull.Value)
                        {
                            item.SetValue(t, value, null);
                        }
                    }
                }
                list.Add(t);
            }
            return list;
        }

    }
}
